﻿using System.Windows;
using YwangHRMSys.DAL;
using YwangHRMSys.Model;
using YwangHRMSys.UI.EmployeeMgr.AddressInfoMgr;


namespace YwangHRMSys.UI.EmployeeMgr
    {
        /// <summary>
        ///     EmployeeWindowList.xaml 的交互逻辑
        /// </summary>
        public partial class EmployeeWindowList : Window
            {

                public EmployeeWindowList()
                {
                    InitializeComponent();
                }


                /// <summary>
                ///     添加员工信息
                /// </summary>
                /// <param name="sender"></param>
                /// <param name="e"></param>
                private void BtnAdd_Click( object sender, RoutedEventArgs e )
                {

                    var win = new EmployeeWindowEdit();
                    win.IsInsert = true;
                    if (win.ShowDialog() == true)
                        {
                            LoadData();
                        }

                }
                #region  载入所有信息
              
                /// <summary>
                ///     载入所有信息
                /// </summary>
                private void LoadData()
                {
                    //部门
                    ColDeaprtment.ItemsSource = new DepartmentInfoDAL().ListAll();

                    ColCmbGender.ItemsSource = new CategoryDAL().GetByCategory("性别");
                    ColPost.ItemsSource = new CategoryDAL().GetByCategory("职位");
                    ColEducation.ItemsSource = new CategoryDAL().GetByCategory("学历");
                    ColPoliticalAffiliation.ItemsSource = new CategoryDAL().GetByCategory("政治面貌");
                    ColMaritalStatus.ItemsSource = new CategoryDAL().GetByCategory("婚姻状况");
                    ColJobTitle.ItemsSource = new CategoryDAL().GetByCategory("职称");
                    //地址管理
                    ColDomicileProvinces.ItemsSource = new EmployeeAddressInfoDAL().ListAll();
                    ColDomicileAddress.ItemsSource = new EmployeeAddressInfoDAL().ListAll();
                    ColNowAddress.ItemsSource = new EmployeeAddressInfoDAL().ListAll();

                    //试用期
                    ColEntryDays.ItemsSource = new TrailDaysInfoDAL().ListAll();
                    ColPositvieDays.ItemsSource = new TrailDaysInfoDAL().ListAll();
                    ColTrailDays.ItemsSource = new TrailDaysInfoDAL().ListAll();

                    ColContractSigned.ItemsSource = new ContractInfoDAL().ListAll();
                    ColContractExpires.ItemsSource = new ContractInfoDAL().ListAll();
                    ColContractLength.ItemsSource = new ContractInfoDAL().ListAll();
                    //员工信息
                    DataEmployeeGrid.ItemsSource = new EmployeeInfoDAL().ListAll();
                    //     DataEmployeeGrid.ItemsSource = new ContractInfoDAL().ListAll();
                }
                #endregion
                private void Window_Loaded( object sender, RoutedEventArgs e )
                {
                    LoadData();


                }

                private void BtnEdit_Click( object sender, RoutedEventArgs e )
                {
                    var model = (EmployeeInfoModel) DataEmployeeGrid.SelectedItem;
                    if (model == null)
                        {
                            MessageBox.Show("没有选中任何行");
                            return;
                        }

                    var windowEdit = new EmployeeWindowEdit();
                    windowEdit.IsInsert = false;
                    windowEdit.EditGuid = model.Id;
                    if (windowEdit.ShowDialog() == true)
                        {
                            LoadData();
                        }

                
                }

                private void BtnExport_Click( object sender, RoutedEventArgs e )
                {

                }

                /// <summary>
                ///     户籍管理
                /// </summary>
                /// <param name="sender"></param>
                /// <param name="e"></param>
                private void BtnDomicilei_Click( object sender, RoutedEventArgs e )
                {
                    var model = (EmployeeInfoModel) DataEmployeeGrid.SelectedItem;
                    if (model == null)
                        {
                            MessageBox.Show("没有选中任何行");
                            return;
                        }

                    var windowEdit = new AddressEditWindow();

                    windowEdit.EditGuid = model.Id;
                    if (windowEdit.ShowDialog() == true)
                        {
                            LoadData();
                        }
                }

                private void BtnDel_Click(object sender, RoutedEventArgs e)
                {
                    EmployeeInfoModel empModel = (EmployeeInfoModel)DataEmployeeGrid.SelectedItem;
                    if (empModel == null)
                    {
                        MessageBox.Show("没有选中任何行");
                        return;
                    }
                    if (MessageBox.Show("真的要删除" + empModel.Name + "吗？", "提醒", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                    {
                        EmployeeInfoDAL dal = new EmployeeInfoDAL();
                        dal.DeleteById(empModel.Id);

                        var contractDal = new ContractInfoDAL();
                        contractDal.DeleteById(empModel.Id);

                     
                        LoadData();
                    }
                }

            }
    }